package com.fancy.learncenter.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.ui.view.FlowLayout;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {

    FlowLayout flowLayout;
    @Bind(R.id.flow_hot)
    FlowLayout flowHot;
    @Bind(R.id.search_et)
    EditText searchEt;


    List<String> recoderText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search, Status.STATUS);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        flowLayout = (FlowLayout) findViewById(R.id.flow);

        recoderText = new ArrayList<>();
        String recoderStr = SPUtils.getSearchRecord();

        if (!TextUtils.isEmpty(recoderStr)) {
            Gson gson = new Gson();
            recoderText = gson.fromJson(recoderStr, new TypeToken<List<String>>() {
            }.getType());
            for (int i = 0; i < recoderText.size(); i++) {
                addTv(recoderText.get(i));
            }

        }


        for (int i = 0; i < 4; i++) {
            addHotTv(texts[i]);
        }

    }


    private void addTv(final String recorde) {
        int ranHeight = dip2px(this, 30);
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(dip2px(this, 10), dip2px(this, 10), dip2px(this, 10), 0);
        TextView tv = new TextView(this);
        tv.setTextAppearance(this, R.style.font_search);
        tv.setBackgroundResource(R.drawable.search_font_bg);
        tv.setText(recorde);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchActivity.this, MallCommonH5Activity.class);
                intent.putExtra(MallCommonH5Activity.URL_KEY, Constant.getSearchUrl(recorde));
                startActivity(intent);
            }
        });
        flowLayout.addView(tv, lp);
    }

    @Override
    public void finish() {
        super.finish();
        Gson gson = new Gson();
        String jsonStr = gson.toJson(recoderText);
        SPUtils.putSearchRecord(jsonStr);
    }

    private void addHotTv(final String recorde) {
        int ranHeight = dip2px(this, 30);
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(dip2px(this, 10), dip2px(this, 10), dip2px(this, 10), 0);
        TextView tv = new TextView(this);
        tv.setTextAppearance(this, R.style.font_search);
        tv.setBackgroundResource(R.drawable.search_font_bg);
        tv.setText(recorde);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchActivity.this, MallCommonH5Activity.class);
                intent.putExtra(MallCommonH5Activity.URL_KEY, Constant.getSearchUrl(recorde));
                startActivity(intent);
            }
        });
        flowHot.addView(tv, lp);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    String[] texts = new String[]{
            "冰雪情缘", "喜羊羊与灰太狼", "小红帽", "倒霉熊",
            "西游记", "葫芦娃七兄弟", "熊出没", "神奇宝贝", "火影忍者",
    };

    @OnClick({R.id.back, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.search:

                if (!TextUtils.isEmpty(searchEt.getText())) {
                    Intent intent = new Intent(this, MallCommonH5Activity.class);
                    intent.putExtra(MallCommonH5Activity.URL_KEY, Constant.getSearchUrl(searchEt.getText().toString()));
                    startActivity(intent);
                } else {
                    ToastUtil.show("搜索不能为空");
                }

                break;
        }
    }

    int lagerRcoder = 3;

    @Override
    protected void onPause() {
        super.onPause();
        if (!TextUtils.isEmpty(searchEt.getText())) {
            if (recoderText.size() >= lagerRcoder) {
                recoderText.remove(lagerRcoder - 1);
                recoderText.add(searchEt.getText().toString());

                TextView textView = (TextView) flowLayout.getChildAt(lagerRcoder - 1);
                textView.setText(searchEt.getText().toString());
            } else {
                recoderText.add(searchEt.getText().toString());
                addTv(searchEt.getText().toString());
            }
        }
    }
}
